import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Users from '@/components/user/Users'
import Welcome from '@/components/Welcome'
import Rights from '@/components/power/Rights'
import Roles from '@/components/power/Roles'
import Cate from '@/components/goods/Cate'
import Params from '@/components/goods/Params'
import GoodsList from '@/components/goods/List'
import Add from '@/components/goods/Add'
import Order from '@/components/order/Order'
Vue.use(Router)

const router=new Router({
  routes: [
    {path: '/', redirect:"/login" },
    {path: '/login', name: 'Login', component: Login },
    {path: '/home', name: 'Home', component: Home, redirect:'/welcome',
      children:[{path: '/welcome', name: 'Welcome', component: Welcome},
                 {path: '/users', name: 'Users', component: Users },
                 {path: '/rights', name: 'Rights', component: Rights },
                 {path: '/roles', name: 'Roles', component: Roles },
                 {path: '/categories', name: 'Cate', component: Cate },
                 {path: '/params', name: 'Params', component: Params },
                 {path: '/goods',  component: GoodsList },
                 {path: '/goods/add',  component: Add },
                 {path: '/orders',  component: Order }
                ]
  },
    
  ]
})
//拦截
router.beforeEach((to,from,next)=>{
  // to:要访问的路径
  // from 代表从哪个路径转跳来
  // next 是一个函数，表述放行
  // next()放行，
  // next('/login')强制转跳
  if(to.path==='/login')
      return next();
  //获取token
  const tokenStr=window.sessionStorage.getItem('token')
  if(!tokenStr)
    return next('/login')
  next()
})
export default router


// export default new Router({
//   routes: [
//     {path: '/', redirect:"/login" },
//     {path: '/index', name: 'Index', component: Index },
//     {path: '/users', name: 'List', component: List },
//     {path: '/students', name: 'List', component: StudentList },
//     {path: '/login', name: 'Login', component: Login },
//     {path: '/home', name: 'Home', component: Home }
//   ]
// })

